package demo.xlj.yuyi.studentapp.shetuanshencha;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.common.CommunityStyleActivity;
import demo.xlj.yuyi.studentapp.common.FuZhongStyleActivity;
import demo.xlj.yuyi.studentapp.common.LookAnnouncementActivity;
import demo.xlj.yuyi.studentapp.common.LookMyTongZhiActivity;
import demo.xlj.yuyi.studentapp.common.MyCommicationActivity;
import demo.xlj.yuyi.studentapp.common.SendTongZhiChooseActivity;
import demo.xlj.yuyi.studentapp.common.SocialActivity;
import demo.xlj.yuyi.studentapp.common.StudentStyleActivity;
import demo.xlj.yuyi.studentapp.common.WhereSchoolActivity;
import demo.xlj.yuyi.studentapp.common.YouthLeagueNewsActivity;
import demo.xlj.yuyi.studentapp.login.StudentMeetLoginActivity;
import demo.xlj.yuyi.studentapp.utils.ActivityUtil;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;

/**
 * 作者：项路俊 on 2018/4/3 0003
 * 邮箱：ThanksForCode@gmail.com
 * 介绍：审查部门的新界面
 */

public class ShenCActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_Shen_Cha)
    ImageView mImageViewShenCha;
    @BindView(R.id.LInearLayout_Shen_Cha_One)
    LinearLayout mLInearLayoutShenChaOne;
    @BindView(R.id.LInearLayout_Shen_Cha_Two)
    LinearLayout mLInearLayoutShenChaTwo;
    @BindView(R.id.LInearLayout_Shen_Cha_Three)
    LinearLayout mLInearLayoutShenChaThree;
    @BindView(R.id.LInearLayout_Shen_Cha_Four)
    LinearLayout mLInearLayoutShenChaFour;
    @BindView(R.id.LInearLayout_Shen_Cha_Five)
    LinearLayout mLInearLayoutShenChaFive;
    @BindView(R.id.LInearLayout_Shen_Cha_Six)
    LinearLayout mLInearLayoutShenChaSix;
    @BindView(R.id.LInearLayout_Shen_Cha_Seven)
    LinearLayout mLInearLayoutShenChaSeven;
    @BindView(R.id.LInearLayout_Shen_Cha_Eight)
    LinearLayout mLInearLayoutShenChaEight;
    @BindView(R.id.LInearLayout_Shen_Cha_Nine)
    LinearLayout mLInearLayoutShenChaNine;
    @BindView(R.id.LInearLayout_Shen_Cha_Ten)
    LinearLayout mLInearLayoutShenChaTen;
    @BindView(R.id.LInearLayout_Shen_Cha_Eleven)
    LinearLayout mLInearLayoutShenChaEleven;
    @BindView(R.id.LInearLayout_Shen_Cha_Twlen)
    LinearLayout mLInearLayoutShenChaTwlen;
    @BindView(R.id.NavigationView_Shen_Cha)
    NavigationView mNavigationViewShenCha;
    @BindView(R.id.Drawer_Layout_Shen_Cha)
    DrawerLayout mDrawerLayoutShenCha;
    @BindView(R.id.Imageswitcher_she_tuan_shen_cha)
    ImageSwitcher mImageswitcherSheTuanShenCha;
    private int[] images = {R.drawable.lunbo4, R.drawable.lunbo5, R.drawable.lunbo6, R.drawable.lunbo7};//图片资源数组
    private int position = 0;

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 11:
                    position++;
                    mImageswitcherSheTuanShenCha.setImageResource(images[position]);
                    if (position == 3) {
                        position = -1;
                    }
                    myHandler.sendEmptyMessageDelayed(11, 4000);

                    break;
            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shen_cha);
        ButterKnife.bind(this);
        String name = (String) SharePerferencesUtils.get(ShenCActivity.this, "name", "", 0);
        ToastUtil.success(ShenCActivity.this, "欢迎" + name + "归来！", true, true);
        initImageView();
        View view = mNavigationViewShenCha.getHeaderView(0);

        TextView textView_Name = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Name);
        TextView textView_ShenFen = (TextView) view.findViewById(R.id.TextView_Chou_Ti_Shen_Fen);

        textView_Name.setText("用户姓名:"+name);
        textView_ShenFen.setText("用户身份：学生会审查部");

        initListener();
    }

    private void initListener() {
        if (mNavigationViewShenCha != null) {
            mNavigationViewShenCha.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    int id = item.getItemId();

                    if (id == R.id.nav_camera) {
                        //表示退出登录
                        ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, StudentMeetLoginActivity.class);
                        ShenCActivity.this.finish();
                    } else if (id == R.id.nav_gallery) {
                        //表示我收藏的社团
                        ActivityUtil.openActivityFromLeftToRightByResult(ShenCActivity.this, CommunityStyleActivity.class, "type", "1");
                    } else if (id == R.id.nav_slideshow) {
                        //表示我的发布
                        ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, LookMyTongZhiActivity.class);
                    }

                    return true;
                }
            });
        }

        if (mImageViewShenCha != null) {
            mImageViewShenCha.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDrawerLayoutShenCha.openDrawer(GravityCompat.START);
                }
            });
        }
    }

    //初始化，上面的轮播图
    private void initImageView() {
        mImageswitcherSheTuanShenCha.setFactory(new ViewSwitcher.ViewFactory() {//设定工厂，每进来一个图片都用一个ImageView接收
            @Override
            public View makeView() {
                ImageView imageView = new ImageView(ShenCActivity.this);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                return imageView;
            }
        });//这个设置Factory是必须的
        mImageswitcherSheTuanShenCha.setImageResource(images[position]);//设置初始图片,也是通过这个代码设置在控件上显示的图片
        mImageswitcherSheTuanShenCha.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.lunbo_in));
        mImageswitcherSheTuanShenCha.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.lunbo_out));//动画淡出

        myHandler.sendEmptyMessageDelayed(11, 3000);

        mImageswitcherSheTuanShenCha.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击事件
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, WhereSchoolActivity.class);
            }
        });
    }


    @OnClick({ R.id.LInearLayout_Shen_Cha_One, R.id.LInearLayout_Shen_Cha_Two, R.id.LInearLayout_Shen_Cha_Three, R.id.LInearLayout_Shen_Cha_Four, R.id.LInearLayout_Shen_Cha_Five, R.id.LInearLayout_Shen_Cha_Six, R.id.LInearLayout_Shen_Cha_Seven, R.id.LInearLayout_Shen_Cha_Eight, R.id.LInearLayout_Shen_Cha_Nine, R.id.LInearLayout_Shen_Cha_Ten, R.id.LInearLayout_Shen_Cha_Eleven, R.id.LInearLayout_Shen_Cha_Twlen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.LInearLayout_Shen_Cha_One:
                //附中风采
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, FuZhongStyleActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Two:
                //社团风貌
                ActivityUtil.openActivityFromLeftToRightByResult(ShenCActivity.this, CommunityStyleActivity.class,"type","0");
                break;
            case R.id.LInearLayout_Shen_Cha_Three:
                //附中要闻
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, YouthLeagueNewsActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Four:
                //社团活动
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, SocialActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Five:
                //学生会风采
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, StudentStyleActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Six:
                //公告通知
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, LookAnnouncementActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Seven:
                //发布通知
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, SendTongZhiChooseActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Eight:
                //待审核
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, AllowOrNotHuoDongActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Nine:
                //我的
                ActivityUtil.openActivityForResultFromLeftToRight(ShenCActivity.this, MyCommicationActivity.class, 99);
                break;
            case R.id.LInearLayout_Shen_Cha_Ten:
                ToastUtil.info(ShenCActivity.this,"暂没有更多",true,true);
                break;
            case R.id.LInearLayout_Shen_Cha_Eleven:
                //附中要闻
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, YouthLeagueNewsActivity.class);
                break;
            case R.id.LInearLayout_Shen_Cha_Twlen:
                //社团活动
                ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, SocialActivity.class);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99 && resultCode == 88) {
            ActivityUtil.openActivityFromLeftToRight(ShenCActivity.this, StudentMeetLoginActivity.class);
            ShenCActivity.this.finish();
//            Log.d("xljkxlxj","执行了1");
        }
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)
        {

            if((System.currentTimeMillis()-exitTime) > 2000)  //System.currentTimeMillis()无论何时调用，肯定大于2000
            {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else
            {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
